//
//  ItemView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/27.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class ItemView: UIView {
    
    // MARK: 赋值
    public var titleLabels:[String]?
    // MARK: 描述
    public var detailLabels:[String]? {
        didSet {
            updateLayout()
        }
    }
    public var isShowDetail:Bool = false
    // MARK: 回调
    public var complete:((_ time:Int) -> Void)?
    
    init(frame: CGRect,count:Int) {
        super.init(frame: frame)
        init_view(num: count)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var commonViews = [CommonView]()
    
    private func init_view(num:Int) -> Void {
        (0..<num).forEach { (i) in
            let common = CommonView()
            commonViews.append(common)
        }
    }
    
    override func layoutSubviews() {
        if commonViews.count == 0 {
            return
        }
        guard let titles = titleLabels else {
            return
        }
        for i in 0..<titles.count {
            // 文本、箭头
            let common = commonViews[i]
            common.tag = i
            common.title = titles[i]
            common.isShowDetail = isShowDetail
            let commonY = CGFloat(i) * MARGIN70
            let commonW = frame.width - MARGIN * 2
            common.frame = CGRect(x: MARGIN, y: commonY, width: commonW, height: MARGIN70)
            addSubview(common)
            // 添加点击事件
            common.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickItem(tap:))))
            // 描述
            if isShowDetail {
                common.detail = detailLabels?[i]
            }
            // 下划线
            let line = UIView()
            if i != titles.count - 1 {
                line.backgroundColor = COLOR_LINE
                line.frame = CGRect(x: MARGIN, y: common.frame.maxY, width: commonW, height: 1)
                addSubview(line)
            }
        }
    }
    
    @objc func clickItem(tap:UITapGestureRecognizer) -> Void {
        if let callback = complete ,let index = tap.view?.tag{
            callback(index)
        }
    }
    
    
    class CommonView: UIView {
        
        // 文本
        private let label = UILabel(title: "测试", color: COLOR_NICE, size: 15)
        // 箭头
        private let arrowView = UIImageView(image: UIImage(named: "arrow_6x11"))
        // 描述
        private let detailLabel = UILabel(title: "测试", color: UIColor.gray, size: 14,align: .right)
        
        public var title:String? {
            didSet {
                label.text = title
            }
        }
        public var detail:String? {
            didSet {
                detailLabel.text = detail
            }
        }
        public var isShowDetail:Bool = false
        
        override func layoutSubviews() {
            // 文本
            label.isUserInteractionEnabled = false
            label.frame = CGRect(x: 0, y: 0, width: MARGIN70, height: frame.height)
            addSubview(label)
            // 箭头
            let arrowX = frame.width - arrowView.bounds.width
            let arrowY = (MARGIN70 - arrowView.bounds.height) * 0.5
            arrowView.frame.origin = CGPoint(x: arrowX, y: arrowY)
            addSubview(arrowView)
            // 描述
            if isShowDetail {
                detailLabel.text = detail
                detailLabel.isUserInteractionEnabled = false
                let detailW = frame.width - label.frame.maxX - MARGIN10 - arrowView.bounds.width
                detailLabel.frame = CGRect(x: label.frame.maxX, y: 0, width: detailW, height: frame.height)
                addSubview(detailLabel)
            }
        }
    }
}
